import { create } from "zustand";

export type useStoreType = {
  isLogin: string | null;
  userId: number;
  setUserId: (value: number) => void;
  setLogin: (value: string) => void;
  setLoginOut: () => void;
  
};
export const useStore = create<useStoreType>((set) => ({
  isLogin:
    typeof localStorage !== "undefined"
      ? localStorage.getItem("token") || null
      : null,
  userId:
    typeof localStorage !== "undefined"
      ? parseInt(localStorage.getItem("userId") as string) || 0
      : 0,
  setLogin: (newValue: string) => {
    localStorage.setItem("token", newValue);
    set({ isLogin: newValue });
  },
  setLoginOut: () => {
    localStorage.clear();
    set({ isLogin: null });
  },
  setUserId: (value: number) => {
    localStorage.setItem("userId", value.toString());
    set({ userId: value });
  },
}));
